/**
 * @file 工单操作-取消
 * @author
 * @date 2020年06月30日16:31:08
 */
layui.define(['layer','laydate','dayjs', 'form', 'list_modify_common', '$http'], function (exports) {
    var $ = layui.$,
        form = layui.form;
    var opCommonUtils = layui.list_modify_common;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var $http = layui.$http;
    var dayjs = layui.dayjs;

    /**
     * 初始化页面
     */
    function initPage() {}

    /**
     * 初始化页面事件
     */
    function initEvent() {

        //提交按钮
        form.on('submit(confirmbtn)', function (data) {
            var url = '/work/modify/save/suspend.action';

            var field = data.field;
            var dutyFailCode = field.dutyFailCode;
            var reasonDesc = field.reasonDesc;
            var nextContactTime = '';
            if (field.nextContactDate) {
                nextContactTime = field.nextContactDate + ' ' + field.nextContactTime;
            }

            if (!dutyFailCode && !reasonDesc) {
                layer.msg("请选择或填写挂起缘由！");
                return false;
            }
            if(!nextContactTime){
                layer.msg("请填写下次联系时间！");
                return false;
            }
            var returnData = {
                orderId: $('#orderId').val(),
                workId: $('#workId').val(),
                dutyFailCode: dutyFailCode,
                reasonDesc: reasonDesc,
                nextContactTime: nextContactTime
            };

            $http.lockSave(url, returnData, data.elem).then(function () {
                layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                    opCommonUtils.goOperateHome();
                });
            }, function (msg) {
                layer.msg(msg);
            });

            return false;
        });


    }

    // 程序入口
    function init() {

        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();
        // 初始化下次联系时间选择框
        renderNextTimeDatePicker();
    }
    /**
     * 初始化下次联系时间选择框
     */
    function renderNextTimeDatePicker () {
        var datePickerElem = '#next-contact-date-input';
        var timePickerElem = '#next-contact-time-input';
        var nowDate = $(datePickerElem).data('date');
        var nextHour = $(timePickerElem).data('hour');
        var minDate = $(datePickerElem).data('today-available')
            ? dayjs(nowDate).add(1, 'day').format('YYYY-MM-DD')
            : dayjs(nowDate).add(2, 'day').format('YYYY-MM-DD');
        var timePickerInstance;

        laydate.render({
            elem: datePickerElem,
            type: 'date',
            format: 'yyyy-MM-dd',
            min: minDate,
            done: function (value) {
                var minHour = nowDate === value ? nextHour : 0;
                if (!timePickerInstance) {
                    timePickerInstance = laydate.render({
                        elem: timePickerElem,
                        type: 'time',
                        format: 'HH:mm:ss',
                        theme: 'time-hour',
                        min: minHour + ':00:00',
                        max: '23:00:00',
                        btns: ['clear', 'confirm']
                    });
                } else {
                    timePickerInstance.config.min.hours = minHour;
                }

                // 切换时间选择框的验证状态
                if (value) {
                    $(timePickerElem).attr('lay-verify', 'required');
                } else {
                    $(timePickerElem).attr('lay-verify', '');
                }
            }
        });
    }

// 初始化
    init();
    exports('oms_work_modify_suspend');
});
